
from django.shortcuts import render
from django.views import View

from .models import ContentCategory
from .utils import get_categories


# 首页视图
class IndexView(View):
    """首页视图"""

    def get(self, request):
        """
        返回首页视图
        :param request: 用户请求
        :return: 首页视图
        """
        # 商品分类数据
        categories = get_categories()

        # 广告数据
        contents = {}
        ad_categories = ContentCategory.objects.all()
        for ad_categorie in ad_categories:
            # 数据结构
            # contents[ad分类名称] = 广告表中的广告对象                      status'是否展示'       '依照权重排序'
            contents[ad_categorie.key] = ad_categorie.content_set.filter(status=True).order_by('sequence')

        context = {
            'categories': categories,
            'contents': contents,
        }
        return render(request, 'index.html', context)
